package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.PestBean;
import com.project.service.IPestService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/**
 * @author RJ
 * @date 2022/9/16 9:22
 */
@RestController
@RequestMapping("pest")
public class PestController {
    @Resource
    private IPestService pestService;

    @RequestMapping("add")
    public String addPest(PestBean pest) {
        pestService.addPest(pest);
        return "ok";
    }

    @RequestMapping("get")
    public PestBean getPestInfo(Integer pestId) {
        PestBean pestBean = pestService.getPestInfo(pestId);
        return pestBean;
    }

    @RequestMapping("info")
    public IPage<PestBean> cutPestByItem(Integer pageNo, Integer pageSize, String pestName, String pestHost) {
        IPage<PestBean> page = pestService.cutPestByItem(pageNo, pageSize, pestName, pestHost);
        return page;
    }

    @PostMapping("upload2")
    public String register2(@RequestParam("file") MultipartFile mf) throws IOException {
        String fileName = mf.getOriginalFilename();
        fileName = System.currentTimeMillis() +
                fileName.substring(fileName.lastIndexOf("."));
        mf.transferTo(new File("g:/LangWo/ForestSystem/image/" + fileName));
        return fileName;
    }
}
